package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin")
public class AdminRoleAndPermission {
    @Resource
    RolePermissionService rolePermissionService;


    @SaAdminCheckPermission("admin:role:add")
    @PostMapping("role")
    public Result addRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.addRole(roleCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:role:search")
    @GetMapping("role/list")
    public Result<PageDTO<RoleDTO>> selectRoleList(PageRequest pageRequest) {
        PageDTO<RoleDTO> roleList = rolePermissionService.getRoleList(pageRequest);
        return Result.ok(roleList);
    }

    @SaAdminCheckPermission("admin:role:update")
    @PutMapping("role")
    public Result updateRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.updateRole(roleCommand);
        return Result.ok();
    }

    @GetMapping("{roleId}/permission/list")
    public Result<RolePermissionDTO> selectPremissionList(@PathVariable("roleId") Long roleId) {
        RolePermissionDTO rolePermissionList = rolePermissionService.getRolePermissionList(roleId);
        return Result.ok(rolePermissionList);
    }

    @SaAdminCheckPermission("admin:role:delete")
    @DeleteMapping("role/{roleId}")
    public Result deleteRole(@PathVariable("roleId") String roleID) {

        if (roleID == null || "undefined".equals(roleID) || roleID.trim().isEmpty()) {
            return Result.fail("角色ID不能为空或无效");
        }

        Long roleId;
        try {
            roleId = Long.valueOf(roleID);
        } catch (NumberFormatException e) {
            return Result.fail("角色ID格式错误，请提供有效的数字ID");
        }
        DeleteRoleOperateDTO deleteRoleOperateDTO = rolePermissionService.deleteRole(roleId);
        return Result.ok(deleteRoleOperateDTO);
    }

    @SaAdminCheckPermission("admin:role:authorize")
    @PutMapping("role/permission")
    public Result updateRolePermission(@RequestBody RolePermissionCommand permissionCommand) {
        rolePermissionService.updateRolePermission(permissionCommand);
        return Result.ok();
    }

    @DeleteMapping("permission/{roleId}/{permissionId}")
    public Result deleteRoleHasPermissionId(@PathVariable("roleId") Long roleId, @PathVariable("permissionId") Long permissionId) {
        DeleteRolePermissionOperateDTO deleteRolePermissionOperateDTO = rolePermissionService.deleteRolePermission(roleId, permissionId);
        return Result.ok();
    }
}
